package com.ruoyi.management.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.base.utils.DateUtils;
import com.ruoyi.management.dao.mapper.AmmeterMapper;
import com.ruoyi.management.dao.mapper.EnergyMapper;
import com.ruoyi.management.pojo.dto.DbInfoDto;
import com.ruoyi.management.pojo.entity.AirConditioningParam;
import com.ruoyi.management.pojo.param.CheckParam;
import com.ruoyi.management.pojo.param.ZtParam;
import com.ruoyi.management.pojo.vo.AmmeterTimeVO;
import com.ruoyi.management.pojo.vo.CheckVO;
import com.ruoyi.management.pojo.vo.EneryVO;
import com.ruoyi.management.pojo.vo.ZTVO;
import com.ruoyi.management.service.EnergyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

@Slf4j
@RequestMapping("/energy")
@RestController
@Api(tags = "能源管理接口")
public class EnergyController extends BaseController {
    @Autowired
    private EnergyService energyService;
    @Autowired
    private EnergyMapper energyMapper;

    @Autowired
    private AmmeterMapper ammeterMapper;

    @ApiOperation(value = "实时数据")
    @GetMapping("/readData")
    public TableDataInfo  readData(
            @RequestParam(value = "dbbh",required = false) String dbbh,
            @RequestParam(value = "qymc",required = false) String qymc,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam("tgszt") String tgszt
    ) {
        Map<String, Object> stringObjectMap = energyService.readData(dbbh, qymc, pageNum, pageSize, tgszt);
        if (ObjectUtils.isEmpty(stringObjectMap) || !stringObjectMap.containsKey("data") || CollectionUtils.isEmpty((Collection<?>) stringObjectMap.get("data"))) {
            return getDataTable(new ArrayList<>());
        }
        return getDataTable((List<?>) stringObjectMap.get("data"), "total", stringObjectMap.get("total").toString());
    }

    @ApiOperation(value = "日电量查询")
    @GetMapping("/dayEnery")
    public AjaxResult  dayEnery(
            @RequestParam(value = "dbbh",required = false) String dbbh,
            @RequestParam(value = "qymc",required = false) String qymc,
            @RequestParam("tgszt") String tgszt,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        if (date == null) {
            // 可以设置默认值，例如当前日期减一天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1);
            date = cal.getTime();
        }
        String zt = null;
        // 当前账号非admin账号
        if (StringUtils.isNotBlank(tgszt) && !"1".equals(tgszt)) {
            ZtParam ztParam = new ZtParam();
            ztParam.setTgszt(tgszt);
            List<ZTVO> ztvos = ammeterMapper.selectZtByNra(ztParam);
            if (CollectionUtils.isNotEmpty(ztvos)) {
                zt = ztvos.get(0).getNr();
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<EneryVO> billingVOList=energyService.Ccompute(dbbh,qymc,date, zt);
        PageInfo<EneryVO> list = new PageInfo<>(billingVOList);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "日分时电量查询")
    @GetMapping("/hourEnery")
    public AjaxResult  hourEnery(
            @RequestParam(value = "dbbh",required = false) String dbbh,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date
    ) {
        List<Map<String, Object>> list=energyService.dayEnergy(dbbh,date);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "月电量查询")
    @GetMapping("/monthEnery")
    public AjaxResult  monthEnery(
            @RequestParam(value = "dbbh",required = false) String dbbh,
            @RequestParam(value = "qymc",required = false) String qymc,
            @RequestParam("tgszt") String tgszt,
            @DateTimeFormat(pattern = "yyyy-MM") Date month,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        String zt = null;
        // 当前账号非admin账号
        if (StringUtils.isNotBlank(tgszt) && !"1".equals(tgszt)) {
            ZtParam ztParam = new ZtParam();
            ztParam.setTgszt(tgszt);
            List<ZTVO> ztvos = ammeterMapper.selectZtByNra(ztParam);
            if (CollectionUtils.isNotEmpty(ztvos)) {
                zt = ztvos.get(0).getNr();
            }
        }
        PageHelper.startPage(pageNum, pageSize);
        List<EneryVO> list=energyService.selectmouth(dbbh,qymc,month, zt);
        if (list==null){
            return AjaxResult.success(list);
        }
        PageInfo<EneryVO> list1 = new PageInfo<>(list);
        return AjaxResult.success(list1);
    }

    @ApiOperation(value = "月分日电量查询")
    @GetMapping("/monthDayEnery")
    public AjaxResult  monthDayEnery(
            @RequestParam(value = "dbbh",required = false) String dbbh,
            @DateTimeFormat(pattern = "yyyy-MM") Date month
    ) {
        List<Map<String, Object>> list=energyService.monthDayEnery(dbbh,month);
        return AjaxResult.success(list);
    }


    /**
     * 查询年度电量
     * @param dbInfoDto 电表查询信息
     * @return 电量结果
     */
    @ApiOperation(value = "年度电量")
    @GetMapping("/yearEnery")
    public AjaxResult selectYearEnergy(DbInfoDto dbInfoDto) {
        List<EneryVO> list = energyService.selectYearEnergy(dbInfoDto);
        if (CollectionUtils.isEmpty(list)) {
            return AjaxResult.success("没有查到相关数据");
        }
        PageInfo<EneryVO> resList = new PageInfo<>(list);
        return AjaxResult.success(resList);
    }

    @ApiOperation(value = "年分月")
    @GetMapping("/yearMonthEnery")
    public AjaxResult  yearMonthEnery(
            @RequestParam(value = "dbbh",required = false) String dbbh,
            @DateTimeFormat(pattern = "yyyy") String year
    ) {
        List<Map<String, Object>> list=energyService.yearMonthEnery(dbbh,year);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "核查联电量列表")
    @GetMapping("/checkEnergy/list")
    public AjaxResult  checkEnergyList(
            @RequestParam(value = "year",required = false) String year,
            @RequestParam(value = "zt",required = false) String zt,
            @RequestParam("tgszt") String tgszt,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<CheckVO> list=energyService.checkEnergyList(year,zt,tgszt);
        PageInfo<CheckVO> list1 = new PageInfo<>(list);
        return AjaxResult.success(list1);
    }

    @ApiOperation(value = "获取载体月度电量")
    @GetMapping("/carrierEnergy")
    public AjaxResult getZtMonthEnery(
            @RequestParam("month") String month,
            @RequestParam(value = "zt",required = false) String zt
    ) {
        Date date= DateUtils.getFirstDayOfMonth(month);
            BigDecimal dbzy=energyMapper.selectdbzyc(date,zt);
            return AjaxResult.success(dbzy);
    }

    @ApiOperation(value = "获取载体月度电量")
    @PostMapping("/checkEnergy")
    public AjaxResult checkEnergy(@RequestBody CheckParam checkParam)
    {
        Boolean a=energyService.checkEnergy(checkParam);
        if (a){
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.success("添加失败");
    }

    @ApiOperation(value = "空调费计算")
    @PostMapping("/airConditioning")
    public AjaxResult  airConditioning(@RequestBody List<AirConditioningParam> param) {
        Boolean a=energyService.airConditioning(param);
        if (!a){
            return AjaxResult.error("保存失败");
        }
        return AjaxResult.success("空调保存成功");
    }


    private LocalDate parseToLocalDate(String monthStr) {
        try {
            return LocalDate.parse(monthStr + "-01");
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid month format. Expected format: yyyy-MM", e);
        }
    }



}
